﻿/*
验证库核心类
author:wx
*/
//声明txjs命名空间
if (!txjs)
{
    var txjs = {};
}
//声明txjs命名空间
if (!txjs.va)
{
   txjs.va = {};
}

//va.validate
(function() {

    //验证规则对象
    txjs.va.rules = {};


    //验证对象
    //public
    //vamanager
    function vamanager() {
        this.all = {}; //存储所有验证实例
        this.errs = {}; //错误信息
        this.errmode = "alert"; //错误消息显示模式
        this.isdone = true; //是否通过验证
    }


    //设置错误消息显示模式
    //@param {string} mode 模式名称alert、append、errcontent
    vamanager.prototype.seterrMode = function(mode) {
        this.errmode = mode;
    };

    //返回当前输入框是否验证通过
    vamanager.prototype.getisok = function() {
        return this.isdone;
    };

    //获取验证实例
    //@param {string} id 需要验证输入框的id
    //@param {boolean} isrequired 是否是必填项 
    //@param {Object/string} errobj 显示错误消息的容器
    vamanager.prototype.getValidate = function(id, errobj) {
        //GLog.write("要验证的输入框是:" + id);
        this.all[id] = new validate(id, errobj, this.errmode);
        return this.all[id];
    };

    //通过添加配置文件创建验证对象
    //@param {object} option 直接添加规则
    vamanager.prototype.addoption = function(option) {
        if (!option) {
            throw "请传入验证规则！";
        }

        for (var x in option) {

            var errobj;

            if (this.errmode == "errcontent") {
                if (option[x]["errcontent"] == undefined) {
                    alert("请在配置中指定errcontent");
                    throw "请在配置中指定errcontent";
                }

                errobj = option[x]["errcontent"];
            }


            //创建验证对象
            var temp = new validate(x, errobj, this.errmode);

            for (var y in option[x]) {
                if (y == "param") {
                    //GLog.write("添加附加参数:" + this.vaoption[x][y]);
                    //alert(this.vaoption[x][y]);
                    temp.setparam(option[x][y]);
                    //GLog.write("设置参数结束");
                }
                if (y == "errcontent") {
                    continue;
                }
                if (y != "param") {
                    //GLog.write("调用add方法，并注册错误消息:" +  this.vaoption[x][y]);
                    temp.add(txjs.va.rules[y], option[x][y]);
                    //GLog.write("调用add" + x + "的实例:" + y);
                }
            }

            this.add(temp);

        }

    };

    //将验证对象，添加到验证列表
    vamanager.prototype.add = function(obj) {
        if (!(obj instanceof validate)) {
            alert("传入参数错误");
            return;
        }

        this.all[obj.getid()] = obj; //添加验证对象
    };

    //提交时验证
    vamanager.prototype.validate = function(fn) {
        try {

            for (var x in this.all) {
                this.all[x].exec();
                this.errs[x] = this.all[x].isruned();
                //GLog.write("验证" + x);
            }

            //alert方式时,通过验证
            if (fn && !(typeof fn == "function")) {
                alert("请传入函数");
                this.isdone = false;
            } else {
                if (this.errmode != "alert") {
                    var ispass = true;
                    for (var y in this.errs) {
                        if (!this.errs[y]) {
                            ispass = false;
                            break;
                        }
                    }

                    if (!ispass) {
                        //GLog.write(i + "未通过验证");
                        this.isdone = false;
                    }
                    else {
                        this.isdone = true;
                        fn();
                    }

                }
                else {
                    this.isdone = true;
                    fn();
                }

            }
        }
        catch (e) { //alert方式时，捕获错误以终止程序执行
            //GLog.write("alert验证未通过，终止执行！");
            this.isdone = false;
        }
    };

    //开放验证类
    txjs.va.vamanager = vamanager;


    //验证单个输入框
    //private
    function validate(id, errobj, errmode) {
        if (id == undefined && errobj == undefined && errmode == undefined) {
            //alert("请检查向validate构造器传入的参数！");
        }
        this.id = id; //验证框id
        this.element = document.getElementById(id);
        this.errMode = errmode; //错误消息显示模式
        this.param;
        this.rules = {}; //验证规则列表
        this.errobj = errobj; //错误消息显示容器
        this.iserr = true; //是否通过验证状态
        this.isexec = false; //是否执行过exec方法
        this.startfn; //开始验证前的函数
        this.endfn; //验证结束后执行
        this.errmsg = ""; //错误消息
    }

    //设置开始执行回调
    //@param {function} fn 验证结束后执行
    validate.prototype.setstart = function(fn) {
        this.startfn = fn;
    };

    //设置结束执行回调
    //@param {function} fn 验证结束后执行
    validate.prototype.setend = function(fn) {
        this.endfn = fn;
    };

    //添加具体的验证规则
    //@param {string} key 对象唯一索引
    //@param {object} obj 验证规则对象
    //@errmsg{string} errmsg 错误提示
    validate.prototype.add = function(obj, errmsg) {
        ////GLog.write("添加具体验证规则:" + this.id);
        if (obj == undefined && errmsg == undefined) {
            alert("请检查add方法，传入的参数！");
        }
        var key = "" + Math.random();
        this.rules[key] = [obj, errmsg];
    };

    //获取验证对象id
    validate.prototype.getid = function() {

        if (this.id != undefined) {
            return this.id;
        }
    };

    //附加参数
    //@param {string} param 额外参数，将传入验证规则对象
    validate.prototype.setparam = function(param) {
        //GLog.write("添加参数");
        this.param = param;
    };

    //执行exec后，判断是否通过验证
    validate.prototype.isruned = function() {

        if (!this.isexec) {
            //alert("尚未执行验证过程");
            return -1;
        }
        return this.iserr;

    };

    //是否执行过验证
    validate.prototype.isvalidated = function() {
        return this.isexec;
    };

    //获取错误消息
    validate.prototype.geterrmsg = function() {

        return this.errmsg;
    };

    //执行验证
    validate.prototype.exec = function() {
        try { //alert弹出错误消息时，捕获alert抛出的异常
            //GLog.write("验证前");
            //GLog.write("执行验证");
            if (this.startfn) {
                this.startfn();
            }
            for (var x in this.rules) {
                //GLog.write("验证关键字：" + x
                var result;
                //alert(this.rules[x][0]);
                if (this.rules[x][0] == txjs.va.rules.required) {
                    result = this.rules[x][0](this.element.value, this.element, this.param);
                }
                else {
                    
                    
                    if (this.element.value != '') {
                       
                        result = this.rules[x][0](this.element.value, this.element, this.param);
                    }
                    else {
                        result = true;
                    }


                }


                //GLog.write("验证结果：" + result);
                if (result) {
                    this.iserr = true;
                    //移除错误
                    continue;
                }
                else {
                    var errmsg = "";
                    this.errmsg = this.rules[x][1];
                    errmsg = this.rules[x][1];
                    //调用错误消息显示类
                    if (this.errMode == "errcontent") {

                        va.ui.errfac(this.errMode, errmsg, this.errobj);

                    } else {
                        //GLog.write("错误:" + errmsg);
                        va.ui.errfac(this.errMode, errmsg, this.element);
                    }
                    this.iserr = false;
                    //GLog.write("显示错误提示:" + this.rules[x][1]);
                    break;
                }
            }

            //执行回调
            
            if (this.iserr) {
                //GLog.write("exec通过验证！");

                if (arguments[0] != undefined) {
                    //GLog.write("执行成功");
                    arguments[0]();
                }
            }
            else {
                //GLog.write("exec未通过验证！");
                if (arguments[1] != undefined) {
                    //GLog.write("失败1");
                    arguments[1]();
                }
            }
            this.isexec = true;
            //GLog.write("验证后");

            if (this.endfn) {
                this.endfn();
            }

        }
        catch (e) {
            this.iserr = false;

            //GLog.write("exec未通过验证！");
            if (arguments[1] != undefined) {
                //GLog.write("执行失败2");
                arguments[1]();
            }
            this.isexec = true;
            //GLog.write("验证后");
            if (this.endfn) {
                this.endfn();
            }
            throw e;
        }
    };

    //获取单个验证实例
    txjs.va.get = function(id, errobj, errmode) {
        ///<summary>获取单个验证对象</summary>
        ///<param name="id">输入框的id</param>
        ///<param name="errobj">指定错误框的id</param>
        ///<param name="errmode">错误显示模式：alert、append、errcontent</param>
        if (id == undefined) {
            alert("必须填写控件id!");
            return false;


        }
        var mode = errmode || "alert";
        var obj = errobj || "";
        return new validate(id, obj, mode);
    };

    //开放验证对象
    txjs.va.validate = validate;
})();